﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonTools;
using MotionIoLib;
using Communicate;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using UserCtrl;
using EpsonRobot;
using BaseDll;
using UserData;

using HalconDotNet;
using System.IO;
using LightControler;
using CameraLib;
using OtherDevice;

using System.Threading.Tasks;
using System.Drawing;
using ModuleCapture;

namespace StationDemo
{
    public static class HalconAlgHelper
    {

        public static bool FindCircleCenterForClib(HObject hoImage,out double RowResult,out double ColResult,out HObject contour)
        {
            RowResult = 0;
            ColResult = 0;
            contour = null;
            return false;
        }
        public static void FindCenterThread(HObject ho_Image, ref double RowResult,
    ref double ColResult)
        {
            try
            {
                 
                double hv_CropLenght = ParamSetMgr.GetInstance().GetDoubleParam($"针间距CropLenght");
                double hv_MinGray = ParamSetMgr.GetInstance().GetDoubleParam($"针间距MinGray");
                double hv_MaxGray = ParamSetMgr.GetInstance().GetDoubleParam($"针间距MaxGray");
                double hv_Closing = ParamSetMgr.GetInstance().GetDoubleParam($"针间距Closing");
                double hv_Opening = ParamSetMgr.GetInstance().GetDoubleParam($"针间距Opening");
                // Local iconic variables 

                HObject ho_GrayImage, ho_Rectangle, ho_ImageReduced;
                HObject ho_Region, ho_RegionClosing, ho_RegionOpening, ho_SelectedRegions;
                HObject ho_Connection = new HObject();
                // Local control variables 

                HTuple hv_Width = null, hv_Height = null, hv_Area = null; HTuple hv_RowResult = null; HTuple hv_ColResult = null;
                HTuple hv_Row = null, hv_Column = null, hv_Area1 = null;
                // Initialize local and output iconic variables 
                HOperatorSet.GenEmptyObj(out ho_GrayImage);
                HOperatorSet.GenEmptyObj(out ho_Rectangle);
                HOperatorSet.GenEmptyObj(out ho_ImageReduced);
                HOperatorSet.GenEmptyObj(out ho_Region);
                HOperatorSet.GenEmptyObj(out ho_RegionClosing);
                HOperatorSet.GenEmptyObj(out ho_RegionOpening);
                HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
                ho_GrayImage.Dispose();
                HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
                HOperatorSet.GetImageSize(ho_GrayImage, out hv_Width, out hv_Height);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Height / 2, hv_Width / 2, 0, hv_CropLenght,
                    hv_CropLenght);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_GrayImage, ho_Rectangle, out ho_ImageReduced);
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, hv_MinGray, hv_MaxGray);
                ho_RegionClosing.Dispose();
                HOperatorSet.ClosingCircle(ho_Region, out ho_RegionClosing, hv_Closing);
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningCircle(ho_RegionClosing, out ho_RegionOpening, hv_Opening);
                HOperatorSet.Connection(ho_RegionOpening,out ho_Connection);
                HOperatorSet.AreaCenter(ho_Connection, out hv_Area, out hv_Row, out hv_Column);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_Connection, out ho_SelectedRegions, "area", "and",
                    hv_Area.TupleMax(), 99999999);
                HOperatorSet.AreaCenter(ho_SelectedRegions, out hv_Area1, out hv_RowResult, out hv_ColResult);
                RowResult = hv_RowResult.D;
                ColResult = hv_ColResult.D;

            }
            catch
            {
                RowResult = 0;
                ColResult = 0;
            }



        }
        static HObject ho_Region, ho_Rect;
        public static bool CheckPlasma(HObject ho_Image, int type, int AreaMax, ref double Area, ref double Lenght)
        {
            try
            {
                HObject ho_reduce;
                HTuple area, row, col;
                HTuple w, h;
                HTuple r1, c1, r2, c2;
                HOperatorSet.GenEmptyObj(out ho_Region);
                HOperatorSet.GenEmptyObj(out ho_Rect);
                HOperatorSet.GenEmptyObj(out ho_reduce);
                HOperatorSet.GetImageSize(ho_Image, out w, out h);
                HOperatorSet.GenRectangle1(out ho_Rect, 0, w / 4, h, w * 3 / 4);
                HOperatorSet.ReduceDomain(ho_Image, ho_Rect, out ho_reduce);
                HOperatorSet.Threshold(ho_Image, out ho_Region, 150, 255);
                HOperatorSet.Connection(ho_Region, out ho_Region);
                HOperatorSet.SelectShape(ho_Region, out ho_Region, "area", "and", 100, 999999);
                HOperatorSet.AreaCenter(ho_Region, out area, out row, out col);
                HOperatorSet.SmallestRectangle1(ho_Region, out r1, out c1, out r2, out c2);
                Lenght = c2.D;
                if (area.D >= Area && area.D <= AreaMax)
                {
                }
                else
                {
                }
                Area = area.D;
                return true;
            }
            catch (Exception e)
            {
                UserTest.LogHelper_AA.Write(e.Message);
                Area = 0;
                return false;
            }
        }
        public static void Find2DCode(HObject ho_Image, bool hv_DarkOnLight, ref string result)
        {

            try
            {
                // Local iconic variables 

                HObject ho_SymbolXLDs;

                // Local control variables 

                HTuple hv_DataCodeHandle = null, hv_ResultHandles = null;
                HTuple hv_DecodedDataStrings = null;
                // Initialize local and output iconic variables 
                HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
                HOperatorSet.SetSystem("filename_encoding", "utf8");
                HOperatorSet.CreateDataCode2dModel("QR Code", new HTuple(), new HTuple(), out hv_DataCodeHandle);
                if (hv_DarkOnLight)
                {
                    HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "polarity", "light_on_dark");
                }
                else
                {
                    HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "polarity", "dark_on_light");
                }

                ho_SymbolXLDs.Dispose();
                HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle, new HTuple(),
                    new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
                result = hv_DecodedDataStrings.S;
                byte[] gb;
                gb = Encoding.GetEncoding("gb2312").GetBytes(result);
                gb = Encoding.Convert(Encoding.GetEncoding("utf-8"), Encoding.GetEncoding("gb2312"), gb);

                result = Encoding.GetEncoding("gb2312").GetString(gb);
            }
            catch
            {
                result = "扫码算法计算失败";
            }
            if (result == "")
                result = "识别扫码为空";
        }

        public static void GlueHighCheck(HObject ho_ImageBefore, HObject ho_ImageAfter, double hv_Row1,
            double hv_Col1, double hv_Row2, double hv_Col2, double hv_MaxGray, double hv_Proportion,
            ref double result)
        {

            try
            {
                // Local iconic variables 

                HObject ho_ImageSub, ho_Rectangle, ho_ImageReduced;
                HObject ho_Region, ho_ConnectedRegions, ho_RegionOpening;
                HObject ho_SelectedRegions, ho_Rectangle1, ho_ImageReduced1;
                HObject ho_Region1, ho_ConnectedRegions1, ho_SelectedRegions1;
                HObject ho_Rectangle2, ho_ImageReduced2;

                // Local control variables 

                HTuple hv_Area = null, hv_Row = null, hv_Column = null;
                HTuple hv_Row11 = null, hv_Column11 = null, hv_Row21 = null;
                HTuple hv_Column21 = null, hv_Area1 = null, hv_Column1 = null;
                HTuple hv_Row3 = null, hv_Column3 = null, hv_Row4 = null;
                HTuple hv_Column4 = null, hv_Row5 = null, hv_Column5 = null;
                HTuple hv_Phi = null, hv_Length1 = null, hv_Length2 = null;
                HTuple hv_Row1_COPY_INP_TMP = hv_Row1;

                // Initialize local and output iconic variables 
                HOperatorSet.GenEmptyObj(out ho_ImageSub);
                HOperatorSet.GenEmptyObj(out ho_Rectangle);
                HOperatorSet.GenEmptyObj(out ho_ImageReduced);
                HOperatorSet.GenEmptyObj(out ho_Region);
                HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
                HOperatorSet.GenEmptyObj(out ho_RegionOpening);
                HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
                HOperatorSet.GenEmptyObj(out ho_Rectangle1);
                HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
                HOperatorSet.GenEmptyObj(out ho_Region1);
                HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
                HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
                HOperatorSet.GenEmptyObj(out ho_Rectangle2);
                HOperatorSet.GenEmptyObj(out ho_ImageReduced2);
                ho_ImageSub.Dispose();
                HOperatorSet.SubImage(ho_ImageAfter, ho_ImageBefore, out ho_ImageSub, 1, 128);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1_COPY_INP_TMP, hv_Col1, hv_Row2,
                    hv_Col2);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_ImageSub, ho_Rectangle, out ho_ImageReduced);
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 0, hv_MaxGray);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);

                ho_RegionOpening.Dispose();
                HOperatorSet.ClosingCircle(ho_ConnectedRegions, out ho_RegionOpening, 5);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.OpeningCircle(ho_RegionOpening, out ho_ConnectedRegions, 5);
                HOperatorSet.AreaCenter(ho_ConnectedRegions, out hv_Area, out hv_Row, out hv_Column);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
                    "and", hv_Area.TupleMax(), 9999999);
                HOperatorSet.SmallestRectangle1(ho_SelectedRegions, out hv_Row11, out hv_Column11,
                    out hv_Row21, out hv_Column21);
                ho_Rectangle1.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle1, ((hv_Row11 + hv_Row21) / 2) - ((hv_Row21 - hv_Row11) / hv_Proportion),
                    hv_Column11, ((hv_Row11 + hv_Row21) / 2) + ((hv_Row21 - hv_Row11) / hv_Proportion),
                    hv_Column21);
                ho_ImageReduced1.Dispose();
                HOperatorSet.ReduceDomain(ho_ImageSub, ho_Rectangle1, out ho_ImageReduced1);
                ho_Region1.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced1, out ho_Region1, 0, hv_MaxGray);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions1);
                HOperatorSet.AreaCenter(ho_ConnectedRegions1, out hv_Area1, out hv_Row1_COPY_INP_TMP,
                    out hv_Column1);
                ho_SelectedRegions1.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions1, "area",
                    "and", hv_Area1.TupleMax(), 9999999);
                HOperatorSet.SmallestRectangle1(ho_SelectedRegions1, out hv_Row3, out hv_Column3,
                    out hv_Row4, out hv_Column4);
                //gen_rectangle1 (Rectangle1, Row3, Column3, Row4, Column4)
                HOperatorSet.SmallestRectangle2(ho_SelectedRegions1, out hv_Row5, out hv_Column5,
                    out hv_Phi, out hv_Length1, out hv_Length2);
                ho_Rectangle2.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle2, hv_Row5, hv_Column5, hv_Phi, hv_Length1,
                    hv_Length2);
                ho_ImageReduced2.Dispose();
                HOperatorSet.ReduceDomain(ho_ImageAfter, ho_Rectangle2, out ho_ImageReduced2);
                result = hv_Length2.D * 2;
            }
            catch { }



        }
        public static void GlueCheck(HObject ho_Image1, HObject ho_Image2, double hv_MinGray,
    double hv_Closing, double hv_MinArea, ref bool Result, ref double RadiusBig,
    ref double Width)
        {

            HTuple hv_RadiusBig = 0.0;
            HTuple hv_Width = 0.0;

            try
            { // Local iconic variables 

                HObject ho_ImageSub, ho_Region, ho_RegionClosing;
                HObject ho_ConnectedRegions1, ho_SelectedRegions1;

                // Local control variables 

                HTuple hv_Row = null, hv_Column = null, hv_Row1 = null;
                HTuple hv_Column1 = null, hv_Number = null;
                // Initialize local and output iconic variables 
                HOperatorSet.GenEmptyObj(out ho_ImageSub);
                HOperatorSet.GenEmptyObj(out ho_Region);
                HOperatorSet.GenEmptyObj(out ho_RegionClosing);
                HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
                HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
                HTuple hv_Result = new HTuple();

                ho_ImageSub.Dispose();
                HOperatorSet.SubImage(ho_Image2, ho_Image1, out ho_ImageSub, 1, 130);
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageSub, out ho_Region, hv_MinGray, 255);
                ho_RegionClosing.Dispose();
                HOperatorSet.ClosingCircle(ho_Region, out ho_RegionClosing, hv_Closing);
                HOperatorSet.SmallestCircle(ho_RegionClosing, out hv_Row, out hv_Column, out hv_RadiusBig);
                HOperatorSet.InnerCircle(ho_RegionClosing, out hv_Row1, out hv_Column1, out hv_Width);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_RegionClosing, out ho_ConnectedRegions1);
                ho_SelectedRegions1.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions1, "area",
                    "and", hv_MinArea, 99999999);
                HOperatorSet.CountObj(ho_SelectedRegions1, out hv_Number);
                if ((int)(new HTuple(hv_Number.TupleGreater(0))) != 0)
                {
                    Result = true;
                }
                else
                {
                    Result = false;
                }

                //connection (RegionClosing, ConnectedRegions)
                //select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999)
                //skeleton (SelectedRegions, Skeleton)
                //gen_contours_skeleton_xld (Skeleton, Contours, 5, 'filter')
                //gen_circle (Circle, Row, Column, RadiusBig)

            }
            catch
            {
                Result = false;
            }
            RadiusBig = hv_RadiusBig.D;
            Width = hv_Width.D;
        }

        public static void Blemish(Bitmap bitmap, double MaxGray, double MinArea, ref int Number,ref bool hv_Result)
        {
            HTuple hv_Number = 0;
            try
            {
                HObject ho_Image = new HObject();
                ImageChangeHelper.Instance.Bitmap2HObject((Bitmap)bitmap.Clone(), ref ho_Image);
                // Local iconic variables 
                HTuple hv_MaxGray = MaxGray;
                HTuple hv_MinArea = MinArea;

                HObject ho_GrayImage2, ho_Region2, ho_ConnectedRegions;
                HObject ho_SelectedRegions;
                // Initialize local and output iconic variables 
                HOperatorSet.GenEmptyObj(out ho_GrayImage2);
                HOperatorSet.GenEmptyObj(out ho_Region2);
                HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
                HOperatorSet.GenEmptyObj(out ho_SelectedRegions);

                ho_GrayImage2.Dispose();
                HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage2);
                ho_Region2.Dispose();
                HOperatorSet.Threshold(ho_GrayImage2, out ho_Region2, 0, hv_MaxGray);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Region2, out ho_ConnectedRegions);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
                    "and", hv_MinArea, 99999);
                HOperatorSet.CountObj(ho_SelectedRegions, out hv_Number);
                if ((int)(new HTuple(hv_Number.TupleGreater(0))) != 0)
                {
                    hv_Result = false;
                }
                else
                {
                    hv_Result = true;
                }
            }
            catch (Exception e)
            {
                hv_Result = true;
            }

            Number = hv_Number.I;

        }

    }
}
